package com.dayuanit.dy15.atm.service.impl;

import com.dayuanit.dy15.atm.entity.User;
import com.dayuanit.dy15.atm.exception.BizException;
import com.dayuanit.dy15.atm.mapper.UserMapper;
import com.dayuanit.dy15.atm.service.UserService;
import com.dayuanit.dy15.atm.util.AtmStringUtils;
import com.dayuanit.dy15.atm.util.MD5Utils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MD5Utils md5Utils;

    @Override
    public void register(String username, String pwd, String confirmPwd) {
        if (StringUtils.isBlank(username) ||
                StringUtils.isBlank(pwd) ||
                StringUtils.isBlank(confirmPwd)) {
            throw new BizException("有必填参数为空");
        }

        if (pwd.length() < 4) {
            throw new BizException("密码长度要大于4");
        }

        if (!pwd.equals(confirmPwd)) {
            throw new BizException("两次密码不相等");
        }

        //TODO 密码强度的校验

        //用户是否存在
        User user = userMapper.getByName(username);
        //这个if判断无法做到100%的去重，在并发的情况下，极有可能会有多个线程通过if判断，这就导致了用户名重复的问题
        //解决方案：对表的用户名字段加唯一索引，让数据库来维护用户名的唯一性。能够解决并发问题。
        //同时，使用唯一索引，还能够解决用户的重复提交问题(面试常问)
        if (null != user) {
            throw new BizException("用户已经存在");
        }

        user = new User();
        user.setPassword(md5Utils.userPassword(pwd, username));
        user.setUsername(username);
        user.setCreateTime(new Date());
        userMapper.insert(user);
    }

    @Override
    public User login(String username, String password) {
        if (StringUtils.isBlank(username) ||
                StringUtils.isBlank(password)) {
            throw new BizException("有必填参数为空");
        }

        User user = userMapper.getByName(username);
        if (null == user) {
            throw new BizException("用户名不存在或者密码错误");
        }

        if (AtmStringUtils.isNotEquals(user.getPassword(), md5Utils.userPassword(password, username))) {
            throw new BizException("用户名不存在或者密码错误");
        }

        return user;
    }

    @Override
    public void resetPwd(long userId, String oldPwd, String newPwd, String confirmPwd) {
        if (StringUtils.isBlank(oldPwd) ||
                StringUtils.isBlank(newPwd) ||
                StringUtils.isBlank(confirmPwd)) {
            throw new BizException("有必填参数为空");
        }

        if (!newPwd.equals(confirmPwd)) {
            throw new BizException("两次密码不相等");
        }

        User user = userMapper.selectByPrimaryKey(userId);
        //校验老密码
        if (!user.getPassword().equals(md5Utils.userPassword(oldPwd, user.getUsername()))) {
            throw new BizException("原始密码错误");
        }

        user.setPassword(md5Utils.userPassword(newPwd, user.getUsername()));

        //最好用乐观锁
        int rows = userMapper.updatePwd(user.getId(), user.getPassword());
        if (1 != rows) {
            throw new BizException("重置密码失败");
        }
    }

    @Override
    public User getUser(long userId) {
        return userMapper.selectByPrimaryKey(userId);
    }
}
